package indi.zhifa.core.common.web.entity.response;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

/**
 *  Description: RESTFUL API返回的统一包装形式
 * @Author: 芝法酱
 * @Create 2021/3/17 10:33
 */
@Data
@Slf4j
@Schema(name = "通用返回")
public class RestResponse<T>{

    @Schema(name = "返回数据")
    protected T data;

    @Schema(name = "返回码，200为正常,非200即为错误")
    protected Integer code;

    @Schema(name = "状态信息")
    protected String message;

    @Schema(name = "详细的状态信息")
    protected String detailMessage;

    public RestResponse() {
        code = HttpStatus.OK.value();
    }

    public RestResponse(T pData) {
        code = HttpStatus.OK.value();
        data = pData;
    }
    public RestResponse(int pCode, String errMsg) {
        code = pCode;
        message = errMsg;
    }
    public RestResponse(int pCode, T pData, String errMsg) {
        code = pCode;
        data = pData;
        message = errMsg;
    }

    public static <T> RestResponse<T> ok(T pData){
        return new RestResponse<T>(pData);
    }

    public static RestResponse error(String pMsgErr){
        return new RestResponse(500,pMsgErr);
    }

    public static RestResponse error(String pMsgErr, String pDetailMsg){
        RestResponse errMsg = new RestResponse(500,pMsgErr);
        errMsg.setDetailMessage(pDetailMsg);
        return errMsg;
    }

    public static RestResponse error(int pCode, String pMsgErr){
        return new RestResponse(pCode,pMsgErr);
    }

}
